package edu.suda.basic.mail.usingPOP3;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/*
 * credit:internet
 * url:http://hi.baidu.com/a393060727/item/42ed32d3d45601e7b3f777e5
 * */
public class SendMail
{
	private String	mailTo					= null;
	private String	mailFrom				= null;
	private String	smtpHost				= null;
	private boolean	debug					= false;
	private String	messageBasePath			= null;
	private String	subject;
	private String	msgContent;
	private Vector<?>	attachedFileList;
	private String	messageContentMimeType	= "text/html; charset=gb2312";
	private String	mailbccTo				= null;
	private String	mailccTo				= null;

	private void fillMail(Session session, MimeMessage msg) throws IOException, MessagingException
	{
		String fileName = null;
		Multipart mPart = new MimeMultipart();
		if (mailFrom != null)
		{
			msg.setFrom(new InternetAddress(mailFrom));
			System.out.println("发送人Mail地址：" + mailFrom);
		}
		else
		{
			System.out.println("没有指定发送人邮件地址！");
			return;
		}
		if (mailTo != null)
		{
			InternetAddress[] address = InternetAddress.parse(mailTo);
			msg.setRecipients(Message.RecipientType.TO, address);
			System.out.println("收件人Mail地址：" + mailTo);
		}
		else
		{
			System.out.println("没有指定收件人邮件地址！");
			return;
		}

		if (mailccTo != null)
		{
			InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
			System.out.println("CCMail地址：" + mailccTo);
			msg.setRecipients(Message.RecipientType.CC, ccaddress);
		}
		if (mailbccTo != null)
		{
			InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
			System.out.println("BCCMail地址：" + mailbccTo);
			msg.setRecipients(Message.RecipientType.BCC, bccaddress);
		}
		msg.setSubject(subject);
		InternetAddress[] replyAddress = { new InternetAddress(mailFrom) };
		msg.setReplyTo(replyAddress);
		// create and fill the first message part
		MimeBodyPart mBodyContent = new MimeBodyPart();
		if (msgContent != null)
			mBodyContent.setContent(msgContent, messageContentMimeType);
		else
			mBodyContent.setContent("", messageContentMimeType);
		mPart.addBodyPart(mBodyContent);
		// attach the file to the message
		if (attachedFileList != null)
		{
			for (Enumeration<?> fileList = attachedFileList.elements(); fileList.hasMoreElements();)
			{
				fileName = (String) fileList.nextElement();
				MimeBodyPart mBodyPart = new MimeBodyPart();

				// attach the file to the message
				FileDataSource fds = new FileDataSource(messageBasePath + fileName);
				System.out.println("Mail发送的附件为：" + messageBasePath + fileName);
				mBodyPart.setDataHandler(new DataHandler(fds));
				mBodyPart.setFileName(fileName);
				mPart.addBodyPart(mBodyPart);
			}
		}
		msg.setContent(mPart);
		msg.setSentDate(new Date());
	}
	public String getMailTo()
	{
		return mailTo;
	}
	public void setMailTo(String mailTo)
	{
		this.mailTo = mailTo;
	}
	public String getMailFrom()
	{
		return mailFrom;
	}
	public void setMailFrom(String mailFrom)
	{
		this.mailFrom = mailFrom;
	}
	public String getSmtpHost()
	{
		return smtpHost;
	}
	public void setSmtpHost(String smtpHost)
	{
		this.smtpHost = smtpHost;
	}
	public boolean isDebug()
	{
		return debug;
	}
	public void setDebug(boolean debug)
	{
		this.debug = debug;
	}
	public String getMessageBasePath()
	{
		return messageBasePath;
	}
	public void setMessageBasePath(String messageBasePath)
	{
		this.messageBasePath = messageBasePath;
	}
	public String getSubject()
	{
		return subject;
	}
	public void setSubject(String subject)
	{
		this.subject = subject;
	}
	public String getMsgContent()
	{
		return msgContent;
	}
	public void setMsgContent(String msgContent)
	{
		this.msgContent = msgContent;
	}
	public Vector<?> getAttachedFileList()
	{
		return attachedFileList;
	}
	public void setAttachedFileList(Vector<?> attachedFileList)
	{
		this.attachedFileList = attachedFileList;
	}
	public String getMessageContentMimeType()
	{
		return messageContentMimeType;
	}
	public void setMessageContentMimeType(String messageContentMimeType)
	{
		this.messageContentMimeType = messageContentMimeType;
	}
	public String getMailbccTo()
	{
		return mailbccTo;
	}
	public void setMailbccTo(String mailbccTo)
	{
		this.mailbccTo = mailbccTo;
	}
	public String getMailccTo()
	{
		return mailccTo;
	}
	public void setMailccTo(String mailccTo)
	{
		this.mailccTo = mailccTo;
	}
	public int sendMail() throws IOException, MessagingException
	{
		Properties props = System.getProperties();
		props.put("mail.smtp.host", smtpHost);
		props.put("mail.smtp.auth", "true");

		MailAuthenticator auth = new MailAuthenticator();

		Session session = Session.getInstance(props, auth);
		session.setDebug(debug);
		MimeMessage msg = new MimeMessage(session);
		Transport trans = null;
		try
		{
			fillMail(session, msg);
			trans = session.getTransport("smtp");
			try
			{
				trans.connect(smtpHost, MailAuthenticator.HUAWEI_MAIL_USER, MailAuthenticator.HUAWEI_MAIL_PASSWORD);																										// HUAWEI_MAIL_PASSWORD);
			}
			catch (AuthenticationFailedException e)
			{
				e.printStackTrace();
				System.out.println("连接邮件服务器错误：");
				return 3;
			}
			catch (MessagingException e)
			{
				System.out.println("连接邮件服务器错误：");
				return 3;
			}
			Transport.send(msg);
			trans.close();
		}
		catch (MessagingException mex)
		{
			System.out.println("发送邮件失败：");
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null)
			{
				System.out.println(ex.toString());
				ex.printStackTrace();
			}
			return 3;
		}
		finally
		{
			try
			{
				if (trans != null && trans.isConnected())
					trans.close();
			}
			catch (Exception e)
			{
				System.out.println(e.toString());
			}
		}
		System.out.println("发送邮件成功！");
		return 0;
	}

	public static void main(String[] argv) throws Exception
	{
       
        SendMail sm = new SendMail();
        sm.setSmtpHost("smtp.sina.com");
        sm.setMailFrom("summerpulse2012@sina.com");
        sm.setMailTo("474736770@qq.com");
        sm.setMsgContent("内容");
        sm.setSubject("标题");
        sm.sendMail();
	}
}
